//
//  OALoginViewController.m
//  maocuhuiOASystem
//
//  Created by michael on 16/3/20.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "MenuViewController.h"
#import "OALoginViewController.h"
#import "MainViewController.h"
#import "XmlParseHelper.h"
#import "webViewController.h"
#import "NewadviceViewController.h"


@interface OALoginViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)NSArray * array;
@property (weak, nonatomic) IBOutlet UILabel *banben;
@property(nonatomic,strong)NSDictionary * dic2;
@end

@implementation OALoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.rememberbutton.selected = YES;
         // Do any additional setup after loading the view.
   NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
    UIImageView *image1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"密码"]];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
 CFShow((__bridge CFTypeRef)(infoDictionary));
// app名称
 NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
 // app版本

 
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
 _banben.text = [NSString stringWithFormat:@"版本:%@",appVersion];
      image1.frame = CGRectMake(10, 5, 20, 20);
    
//image1.transform = CGAffineTransformMakeTranslation(30, 0);
       //image1.contentMode = UIViewContentModeTopLeft;
   UIView *view1 = [[UIView alloc]init];
   view1.backgroundColor = [UIColor whiteColor];
   view1.size = CGSizeMake(30,30);
   [view1 addSubview:image1];
    self.passwordTextField.leftView = view1;
    self.passwordTextField.leftViewMode = UITextFieldViewModeAlways;
UIImageView *image2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"用户名"]];
 image2.frame = CGRectMake(10, 5, 20, 20);
  UIView *view2 = [[UIView alloc]init];
   view2.backgroundColor = [UIColor whiteColor];
   view2.size = CGSizeMake(30,30);
   [view2 addSubview:image2];

 
    self.accountTextField.leftView = view2;
    self.accountTextField.leftViewMode = UITextFieldViewModeAlways;

 if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] length] !=0) {
        self.accountTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    }
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"pwdId"] length] !=0) {
        self.passwordTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwdId"];
    }
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"autoLogin"] isEqualToString:@"Y"]) {
        self.autobutton.selected = YES;
    }
   // 判断版本
//[self upApp];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.accountTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (IBAction)LoginAction:(id)sender {
//      NewadviceViewController    * mainVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Newadvice"];
//        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
//
//self.view.window.rootViewController = navVC;
//
//}
- (IBAction)LoginAction:(id)sender {
[self.accountTextField resignFirstResponder];
[self.passwordTextField resignFirstResponder];
 if (self.accountTextField.text.length == 0) {
        [self.view makeToast:@"请输入账号"];
        return;
    }
    if (self.passwordTextField.text.length == 0) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
MBProgressHUD * hud =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.labelText = @"正在登陆,请稍等";    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
 NSString *strUrl = [self.accountTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];     [params setObject:self.passwordTextField.text forKey:@"pwdId"];
[HttpTools postWithURL:KLoginURL arrays:@[@{@"userId":strUrl},@{@"pwdId":self.passwordTextField.text}] method:@"validationUser" success:^(id JSON) {
       [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        XmlParseHelper * parse = [[XmlParseHelper alloc]initWithData:JSON];
        NSArray * dataArray = [parse selectNodes:@"errmsg"];
        NSString * resultTag = nil;
        if (dataArray.count) {
            NSDictionary * dic  = [dataArray lastObject];
            resultTag = dic[@"text"];
        }
                 MenuViewController *tar = [[MenuViewController alloc]init];

//         MainViewController * mainVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
//        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
    
    
       
    
        switch ([resultTag integerValue]) {
            case 1:
                 [self.view makeToast:@"您的用户名不存在，请确认后再输"];
                break;
                case 2:

               self.view.window.rootViewController = tar;
                if (self.rememberbutton.selected) {
                    [[NSUserDefaults standardUserDefaults] setObject:strUrl forKey:@"userId"];
                    [[NSUserDefaults standardUserDefaults] setObject:self.passwordTextField.text forKey:@"pwdId"];
                 
                }else{
                    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"userId"];
                    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"pwdId"];
                }
                
                if (!self.autobutton.selected) {
                    [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"autoLogin"];
                }else{
                    [[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"autoLogin"];
                }

                break;
                case 3:
                [self.view makeToast:@"您的密码错误，请确认后再输"];
                break;
                case -1099:
                [self.view makeToast:@"抱歉，服务器出错"];
                break;
            default:
                break;
        }
        
    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];

}
- (IBAction)rememberbutton:(id)sender {
 self.rememberbutton.selected = !self.rememberbutton.selected;
    if (!self.rememberbutton.selected) {
        self.autobutton.selected = self.rememberbutton.selected;
    }
    

}

- (IBAction)autonbutton:(id)sender {
    self.autobutton.selected = !self.autobutton.selected;


//在此实现不打勾时的方法
}
@end
